<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">计划名称</label>
						<div class="layui-input-inline">
							<input type="text" name="planName" placeholder="请输入计划全称" class="layui-input" value="">
						</div>
						<label class="layui-form-label">设备名称</label>
						<div class="layui-input-inline">
							<input type="text" name="equipmentName" placeholder="请输入设备全称" class="layui-input" value="">
						</div>
						<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="plan-query">
							<i class="layui-icon layui-icon-search"></i>
							查询
						</button>
						<button type="reset" class="pear-btn pear-btn-md">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="equipment-plan-table" lay-filter="equipment-plan-table"></table>
			</div>
		</div>

	<script type="text/html" id="equipment-plan-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>

	<script type="text/html" id="user-bar">
		<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
<!--		<button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>-->
	</script>

<!--	<script type="text/html" id="user-enable">-->
<!--		<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" checked = "{{ d.enable == 0 ? 'true' : 'false' }}">-->
<!--	</script>-->

	<script type="text/html" id="equipment-useStatus">
		{{#if (d.useStatus == 1) { }}
		<span>未使用</span>
		{{# }else if(d.useStatus == 2){ }}
		<span>使用中</span>
		{{# } }}
	</script>

	<script type="text/html" id="equipment-healthStatus">
		{{#if (d.healthStatus == 1) { }}
		<span>正常</span>
		{{# }else if(d.healthStatus == 2){ }}
		<span>待维修</span>
		{{# } }}
    </script>

	<script type="text/html" id="equipment-price">
		<span>{{ d.price }}元</span>
    </script>

	<script type="text/html" id="equipment-status">
		<span>已审核</span>
	</script>

<!--	<script type="text/html" id="equipment-lastUpdateTime">-->
<!--		{{layui.util.toDateString(d.lastUpdateTime, 'yyyy-MM-dd HH:mm:ss')}}-->
<!--	</script>-->

	<script type="text/html" id="equipment-addTime">
		{{layui.util.toDateString(d.addTime, 'yyyy-MM-dd HH:mm:ss')}}
	</script>

	<div style="display: none;">
		<div class="layer-top">
			<br />
			<h3>上侧弹层内容...</h3>
		</div>
	</div>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'drawer', 'dropdown','common'], function() {
				let layer = layui.layer;
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let drawer = layui.drawer;
				let dropdown = layui.dropdown;
				let common = layui.common;


				let cols = [
					[{
						type: 'checkbox'
					},
						{
							title: '编号',
							field: 'id',
							align: 'center',
							width: 100
						},
						{
							title: '计划名称',
							field: 'planName',
							align: 'center',
							width: 100
						},
						{
							title: '设备名称',
							field: 'equipmentName',
							align: 'center',
							width: 100
						},
						{
							title: '颜色',
							field: 'color',
							align: 'center'
						},
						{
							title: '尺寸',
							field: 'size',
							align: 'center',
							width: 100,
						},
						{
							title: '采购数量',
							field: 'quantity',
							align: 'center'
						},
						{
							title: '是否审核',
							field: 'size',
							align: 'center',
							templet: '#equipment-status'
						},
						{
							title: '计划创建时间',
							field: 'addTime',
							align: 'center',
							templet: '#equipment-addTime'
						}
						// {
						// 	title: '操作',
						// 	toolbar: '#user-bar',
						// 	align: 'left',
						// 	width: 160
						// }
					]
				]

				table.render({
					elem: '#equipment-plan-table',
					url: '/equipmentPlan/queryEquipmentPlanList',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#equipment-plan-toolbar',
					defaultToolbar: [{
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(equipment-plan-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					} 
				});

				table.on('toolbar(equipment-plan-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(plan-query)', function(data) {
					console.log("query param : " + JSON.stringify(data));
					table.reload('equipment-plan-table', {
						where: data.field
					})
					return false;
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增设备计划',
						shade: 0.1,
						area: ['500px', '400px'],
						content: '/equipmentPlan/infoTable/add'
					});

				}

				window.edit = function(obj) {
					console.log(JSON.stringify(obj.data));
					layer.open({
						type: 2,
						title: '编辑',
						shade: 0.1,
						area: ['500px', '400px'],
						content: '/equipment/infoTable/edit?id=' + obj.data.id +
								"&equipmentName=" + obj.data.equipmentName + "&color=" + obj.data.color +
								"&size=" + obj.data.size + "&useAge=" + obj.data.useAge + "&useStatus=" +obj.data.useStatus +
								"&price=" + obj.data.price + "&healthStatus=" + obj.data.healthStatus
					});
				}


				window.remove = function(obj) {
					layer.confirm('确定要删除该用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						let idArray = [];
						console.log(JSON.stringify(obj.data));
						idArray.push(obj.data.id);
						$.ajax({
							url: '/equipment/deleteEquipment',
							data: JSON.stringify(idArray),
							dataType: 'json',
							type: 'post',
							contentType:'application/json',
							success: function(result) {
								layer.close(loading);
								if (result.code === 0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('equipment-table');

									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}


				/**
				 * 批量删除
				 * @param obj
				 * @returns {boolean}
				 */
				window.batchRemove = function(obj) {
					let data = table.checkStatus(obj.config.id).data;
					if (data.length === 0) {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let idArray =data.map(e => e.id);
					layer.confirm('确定要删除这些计划信息么', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: '/equipmentPlan/deleteEquipmentPlan',
							data: JSON.stringify(idArray),
							dataType: 'json',
							contentType:'application/json',
							type: 'post',
							success: function(result) {
								layer.close(loading);
								if (result.code === 0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('equipment-plan-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('equipment-plan-table');
				}
			})
		</script>
	</body>
</html>
